package org.example.mq;

import org.example.config.RabbitConfig;
import org.example.dao.OrderDao;
import org.example.entity.Book;
import org.example.entity.Order;
import org.example.service.BookServiceImpl;
import org.example.service.OrderService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

@Component
public class ReceiverImpl {
    @Autowired
    private OrderService orderService;
    @Autowired
    private BookServiceImpl bookService;

    @RabbitListener(queues = RabbitConfig.S59_QUEUE_1)
    public void receive1(Order order){
        System.out.println("收到订单消息，创建订单"+order.getOrderId());
        Book book=bookService.findById(order.getBookId());
        order.setMoney(book.getMoney().multiply(new BigDecimal("0.1")));
        orderService.createOrder(order);
    }

    //修改库存
    @RabbitListener(queues = RabbitConfig.S59_QUEUE_2)
    public void receive2(Order order){
        System.out.println("收到订单消息，修改库存"+order.getOrderId());
        System.out.println("书籍Id"+order.getBookId());
      bookService.modifyStock(order.getBookId(),-1);
    }

}
